﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class SysCodeDetailDA : ISysCodeDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCRIPTION = "@DESCRIPTION";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string REMARK = "@REMARK";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_SYS_CODE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@SYS_CODE_DETAIL WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE AND SYS_CODE = @SYS_CODE ";
        private string SQL_DELETE_SYS_CODE_DETAIL = " DELETE FROM @_@SYS_CODE_DETAIL WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE AND SYS_CODE = @SYS_CODE ";
        private string SQL_INSERT_SYS_CODE_DETAIL = " INSERT INTO @_@SYS_CODE_DETAIL ( WH_ID, SYS_CODE_TYPE, SEQ_NO, SYS_CODE, DESCRIPTION, REMARK, SYSTEM_CREATED, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @SYS_CODE_TYPE, @SEQ_NO, @SYS_CODE, @DESCRIPTION, @REMARK, @SYSTEM_CREATED, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_SYS_CODE_DETAIL = " SELECT  ROWID, WH_ID, SYS_CODE_TYPE, SEQ_NO, SYS_CODE, DESCRIPTION, REMARK, SYSTEM_CREATED, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@SYS_CODE_DETAIL ";
        private string SQL_UPDATE_SYS_CODE_DETAIL = " UPDATE @_@SYS_CODE_DETAIL SET WH_ID = @WH_ID, SEQ_NO = @SEQ_NO, DESCRIPTION = @DESCRIPTION, REMARK = @REMARK, SYSTEM_CREATED = @SYSTEM_CREATED, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE AND SYS_CODE = @SYS_CODE  ";
        private string SYS_CODE = "@SYS_CODE";
        private string SYS_CODE_TYPE = "@SYS_CODE_TYPE";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckSysCodeDetailIDUnique(string SysCode, string SysCodeType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE", SysCode), new SqlParameter("@SYS_CODE_TYPE", SysCodeType) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_SYS_CODE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckSysCodeDetailIDUnique(DataBase dataBase, DbTransaction tran, string SysCode, string SysCodeType, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE", SysCode), new SqlParameter("@SYS_CODE_TYPE", SysCodeType) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_SYS_CODE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteSysCodeDetail(List<string> sysCodeType, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sqlTemp = " DELETE FROM @_@SYS_CODE_DETAIL WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE ";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCodeType.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeType[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteSysCodeDetail(List<string> sysCodeType, List<string> sysCode, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sqlTemp = this.SQL_DELETE_SYS_CODE_DETAIL;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCode.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeType[j]), new SqlParameter("@SYS_CODE", sysCode[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteSysCodeDetail(DataBase dataBase, DbTransaction tran, List<string> sysCodeType, List<string> whLoginID)
        {
            int result = 0;
            string sqlTemp = " DELETE FROM @_@SYS_CODE_DETAIL WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE ";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCodeType.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeType[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteSysCodeDetail(DataBase dataBase, DbTransaction tran, List<string> sysCodeType, List<string> sysCode, List<string> whLoginID)
        {
            int result = 0;
            string sqlTemp = this.SQL_DELETE_SYS_CODE_DETAIL;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCodeType.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeType[j]), new SqlParameter("@SYS_CODE", sysCode[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllSysCodeDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllSysCodeDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<SysCodeDetailInfo> GetSysCodeDetailByID(string Sys_Code_Type, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID) + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", Sys_Code_Type) };
            List<SysCodeDetailInfo> sysCodeDetailList = new List<SysCodeDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    SysCodeDetailInfo sysCodeDetailInfo = new SysCodeDetailInfo();
                    sysCodeDetailInfo = new SysCodeDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["SYS_CODE"].ToString(), reader["DESCRIPTION"].ToString(), reader["REMARK"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    sysCodeDetailList.Add(sysCodeDetailInfo);
                }
            }
            return sysCodeDetailList;
        }

        public SysCodeDetailInfo GetSysCodeDetailByID(string Sys_Code_Type, string Sys_Code, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID) + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE AND  SYS_CODE = @SYS_CODE";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", Sys_Code_Type), new SqlParameter("@SYS_CODE", Sys_Code) };
            SysCodeDetailInfo sysCodeDetail = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    sysCodeDetail = new SysCodeDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["SYS_CODE"].ToString(), reader["DESCRIPTION"].ToString(), reader["REMARK"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return sysCodeDetail;
        }

        public List<SysCodeDetailInfo> GetSysCodeDetailByID(DataBase dataBase, DbTransaction tran, string Sys_Code_Type, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID) + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", Sys_Code_Type) };
            List<SysCodeDetailInfo> sysCodeDetailList = new List<SysCodeDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                SysCodeDetailInfo sysCodeDetailInfo = new SysCodeDetailInfo();
                sysCodeDetailInfo = new SysCodeDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["SYS_CODE"].ToString(), reader["DESCRIPTION"].ToString(), reader["REMARK"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                sysCodeDetailList.Add(sysCodeDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return sysCodeDetailList;
        }

        public SysCodeDetailInfo GetSysCodeDetailByID(DataBase dataBase, DbTransaction tran, string Sys_Code_Type, string Sys_Code, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID) + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE AND  SYS_CODE = @SYS_CODE";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", Sys_Code_Type), new SqlParameter("@SYS_CODE", Sys_Code) };
            SysCodeDetailInfo sysCodeDetail = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                sysCodeDetail = new SysCodeDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["SYS_CODE"].ToString(), reader["DESCRIPTION"].ToString(), reader["REMARK"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return sysCodeDetail;
        }

        public DataSet GetSysCodeDetailByQueryList(List<string> sqlWhere, SysCodeDetailQueryEntity syscodedetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syscodedetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syscodedetailQuery.CurrentPage, syscodedetailQuery.PageSize, syscodedetailQuery.SortField, syscodedetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetSysCodeDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, SysCodeDetailQueryEntity syscodedetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_SYS_CODE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syscodedetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syscodedetailQuery.CurrentPage, syscodedetailQuery.PageSize, syscodedetailQuery.SortField, syscodedetailQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertSysCodeDetail(SysCodeDetailInfo sysCodeDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCodeDetail_Parameters(sysCodeDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_CODE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertSysCodeDetail(DataBase dataBase, DbTransaction tran, SysCodeDetailInfo sysCodeDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCodeDetail_Parameters(sysCodeDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_CODE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_SysCodeDetail_Parameters(SysCodeDetailInfo sysCodeDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.SYS_CODE_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.SYS_CODE, SqlDbType.VarChar, 20), new SqlParameter(this.DESCRIPTION, SqlDbType.VarChar, 200), new SqlParameter(this.REMARK, SqlDbType.VarChar, 500), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(sysCodeDetailInfo.WhID))
            {
                paramArray[0].Value = sysCodeDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = sysCodeDetailInfo.SysCodeType;
            paramArray[2].Value = sysCodeDetailInfo.SeqNo;
            paramArray[3].Value = sysCodeDetailInfo.SysCode;
            if (!string.IsNullOrEmpty(sysCodeDetailInfo.Description))
            {
                paramArray[4].Value = sysCodeDetailInfo.Description;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysCodeDetailInfo.Remark))
            {
                paramArray[5].Value = sysCodeDetailInfo.Remark;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            paramArray[6].Value = sysCodeDetailInfo.SystemCreated;
            if (!string.IsNullOrEmpty(sysCodeDetailInfo.IsActive))
            {
                paramArray[7].Value = sysCodeDetailInfo.IsActive;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            paramArray[8].Value = sysCodeDetailInfo.CreatedBy;
            paramArray[9].Value = sysCodeDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateSysCodeDetail(SysCodeDetailInfo sysCodeDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCodeDetail_Parameters(sysCodeDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_CODE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateSysCodeDetail(DataBase dataBase, DbTransaction tran, SysCodeDetailInfo sysCodeDetailInfo, List<string> whLoginID)
        {
            return 0;
        }
    }
}

